package com.zsh.rabbitmq.seven;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.zsh.rabbitmq.utils.RabbitMQUtils;

/**
 * @author zsh
 * @Description 主题模式，接收者02
 * @create 2022-05-05 19:07
 */
public class ReceiveLogsTopic02 {

    public static final String EXCHANGE_NAME = "topic_logs";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMQUtils.getChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);

        String queueName = "q2";
        channel.queueDeclare(queueName,false,false,false,null);

        channel.queueBind(queueName,EXCHANGE_NAME,"*.*.rabbit");
        channel.queueBind(queueName,EXCHANGE_NAME,"lazy.#");

        System.out.println("ReceiveLogsTopic02 等待接收消息......");

        //接收消息
        DeliverCallback deliverCallback = (consumerTag, message) ->{
            String msg = new String(message.getBody(),"UTF-8");
            System.out.println("接收队列：" + queueName + "，绑定键为：" + message.getEnvelope().getRoutingKey());
            System.out.println("ReceiveLogsTopic02 接收到的消息是：" + msg);
        };

        //取消消息,先不写

        channel.basicConsume(queueName,true,deliverCallback,(CancelCallback) null);
    }
}
